<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Expression;

class UserBalanceLogTable extends AbstractTable
{
    protected $table = 'hy_user_balance_log';
    protected $id = 'ubl_id';
    
    public function find($query = array())
    {
        $select = new Select(array('b' => $this->getTable()));
        //$select->columns(array(''));
        $select->join(
            array('u' => 'hy_user'),
            'u.user_id=b.user_id',
            array('user_name', 'user_uid', 'balance'),
            $select::JOIN_LEFT
            );
        
        if($query['user_uid']) {
            $select->where(array('u.user_uid' => $query['user_uid']));
        }
        
        if(!empty($query['user_name'])) {
            $spec = function (Where $where) use ($query) {
                $where->like('u.user_name', '%'.$query['user_name']."%");
            };
        
            $select->where($spec);
        }
        
        if($query['start_time'] && $query['end_time']){
            $between = function (Where $where) use ($query) {
                $where->between('b.ubl_create_time', $query['start_time'], $query['end_time']);
            };
            $select->where($between);
        }
        if(!empty($_SESSION['account']['org_id'])) {
            $select->where(array('u.org_id' => $_SESSION['account']['org_id']));
        }
        
        $select->order('b.ubl_id DESC');
        return $this->fetchPaginate($select, $query);
    
    }

    public function findOne($id)
    {
        $rowset = $this->select(array('user_id' => $id));
        $row = $rowset->current();
        return $row;
    }
    public function create($data)
    {
        $data['ubl_create_time'] = new Expression('NOW()');
        $this->insert($data);
        $id = $this->lastInsertValue;
        return $id;
    }
    public function getOutExcelList($query = array())
    {
        $select = new Select(array('b' => $this->getTable()));
        //$select->columns(array(''));
        $select->join(
            array('u' => 'hy_user'),
            'u.user_id=b.user_id',
            array('user_name', 'user_uid', 'balance'),
            $select::JOIN_LEFT
            );
    
        if($query['user_uid']) {
            $select->where(array('u.user_uid' => $query['user_uid']));
        }
    
        if(!empty($query['user_name'])) {
            $spec = function (Where $where) use ($query) {
                $where->like('u.user_name', '%'.$query['user_name']."%");
            };
    
            $select->where($spec);
        }
    
        if($query['start_time'] && $query['end_time']){
            $between = function (Where $where) use ($query) {
                $where->between('b.ubl_create_time', $query['start_time'], $query['end_time']);
            };
            $select->where($between);
        }
        if(!empty($_SESSION['account']['org_id'])) {
            $select->where(array('u.org_id' => $_SESSION['account']['org_id']));
        }
    
        $select->order('b.ubl_id DESC');
        $rowset = $this->selectWith($select);
        $list = $rowset->toArray();
    
        return $list;
    }
}